<!--
 * @Description: 产量查询页面
-->
<template>
  <div class="container">
    <div class="left">
      <div class="title">选择批次</div>
      <el-select
        style="width: 93%; margin-bottom: 10px;"
        clearable
        v-model="selectedYear"
        placeholder="选择年份"
        @change="filterBatchByYear"
      >
        <el-option
          style=" text-align: center;"
          v-for="year in years"
          :key="year"
          :label="year + `年`"
          :value="year"
        >
        </el-option>
      </el-select>
      <ul class="left-content">
        <li
          v-for="item in filteredBatchLists"
          :key="item.batchId"
          :class="{ activeLi: item.batchId === activeBatchId }"
          @click="changeBatch(item)"
        >
          {{ item.batchName }}
        </li>
      </ul>
    </div>
    <div class="right">
      <template>
        <ul
          class="header"
          v-for="item in titleDataList"
          :key="item.batchName"
          v-show="titleDataList.length > 0"
        >
          <li>
            <img src="./image/batchName.png" alt="" />
            <div class="text">批次名称：{{ item.batchName || "暂无数据" }}</div>
          </li>
          <li>
            <img src="./image/output.png" alt="" />
            <div class="text" v-if="item.batchObj">
              总产量：{{ item.batchObj["totalYield"] }}斤
            </div>
            <div class="text" v-else>总产量：暂无数据</div>
          </li>
          <li>
            <img src="./image/acreyield.png" alt="" />
            <div class="text" v-if="item.area">
              种养亩数： {{ item.area }}{{ item.areaUnitName }}
            </div>
            <div class="text" v-else>种养亩数：暂无数据</div>
          </li>
          <li>
            <img src="./image/acreoutput.png" alt="" />
            <div class="text" v-if="item.batchObj">
              亩产量：{{ item.batchObj["averageYield"]
              }}{{ item.batchObj["unitName"] }}
            </div>
            <div class="text" v-else>亩产量：暂无数据</div>
          </li>
        </ul>
        <ul class="header" v-show="titleDataList.length === 0">
          <li>
            <img src="./image/batchName.png" alt="" />
            <div class="text">批次名称："暂无数据"</div>
          </li>
          <li>
            <img src="./image/output.png" alt="" />
            <div class="text">总产量：暂无数据</div>
          </li>
          <li>
            <img src="./image/acreyield.png" alt="" />
            <div class="text">种养亩数：暂无数据</div>
          </li>
          <li>
            <img src="./image/acreoutput.png" alt="" />
            <div class="text">亩产量：暂无数据</div>
          </li>
        </ul>
      </template>
      <div class="right-content">
        <el-table
          class="table"
          v-loading="loading"
          highlight-current-row
          :data="tableData"
          stripe
          height="100%"
          style="width: 100%; height: 100%"
          empty-text="暂无数据"
          border
        >
          <el-table-column type="index" label="序号" width="100" />
          <el-table-column prop="facilityName" label="设施" />
          <el-table-column prop="plantTypeName" label="种养类别">
            <template slot-scope="scope">
              {{ scope.row.standard && scope.row.standard.plantTypeName }}
            </template>
          </el-table-column>
          <el-table-column prop="plantVarietyName" label="种养品种">
            <template slot-scope="scope">
              {{ scope.row.standard && scope.row.standard.plantVarietyName }}
            </template>
          </el-table-column>
          <el-table-column prop="yield" label="产量">
            <template slot-scope="scope">
              {{ scope.row.yield }}
              {{
                scope.row.productionBatchBO &&
                  scope.row.productionBatchBO.assetUnitName
              }}
            </template>
          </el-table-column>
          <el-table-column prop="grade" label="产量品级" />
          <el-table-column prop="yieldDate" label="产出日期" />
        </el-table>
        <div class="noData" v-show="tableData.length === 0">暂无数据</div>
      </div>
    </div>
  </div>
</template>

<script>
import nowpagereq from "@/api/digital-production/production-statistics/output-query";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import batchReq from "@/api/digital-production/production-batch-manage/production-batch-query";
import { mymixin } from "@/utils/mixins";
import { getToken } from "@/utils/auth";
export default {
  name: "OutputQuery",
  components: {
    BaseMainSlot
  },
  mixins: [mymixin],
  data() {
    return {
      batchLists: [],
      filteredBatchLists: [],
      activeBatchId: "",
      titleDataList: [],
      tableData: [],
      nowBatchId: "",
      loading: false,
      selectedYear: null,
      years: []
    };
  },
  mounted() {
    this.loading = true;
    this.getAllBatchList();
  },
  methods: {
    async getAllBatchList() {
    try {
      const response = await batchReq.querybylist({
        organizationNo: sessionStorage.organizationNo,
        simpleData: true
      });
      this.batchLists = response.data;
      this.activeBatchId = response.data[0].batchId;
      this.nowBatchId = response.data[0].batchNo;
      this.getBatchTitle();
      let List = await this.tableDataList(this.nowBatchId);
      this.tableData = List;
      this.addblankmet();
      this.fillYears();
    } catch (error) {
      console.error("Failed to load data:", error);
    } finally {
      this.loading = false;
    }
  },
    fillYears() {
      this.years = this.batchLists
        .map(item => new Date(item.assetDate).getFullYear())
        .filter((year, index, self) => self.indexOf(year) === index)
        .sort();
      if (this.years.length > 0) {
        this.selectedYear = this.years[0];
        this.filterBatchByYear();
        if (this.filteredBatchLists.length > 0) {
          this.changeBatch(this.filteredBatchLists[0]);
        }
      }
    },

    filterBatchByYear() {
      this.filteredBatchLists = this.batchLists.filter(
        item =>
          new Date(item.assetDate).getFullYear() === Number(this.selectedYear)
      );
    },

    async changeBatch(e) {
      this.activeBatchId = e.batchId;
      this.nowBatchId = e.batchNo;
      this.getBatchTitle();
      let List = await this.tableDataList(this.nowBatchId);
      this.tableData = List;
      this.addblankmet();
    },
    // 获取title
    getBatchTitle() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          farmingTypeNo: getToken("farmingTypeNo"),
          organizationNo: sessionStorage.organizationNo,
          batchNo: this.nowBatchId
        }
      };
      nowpagereq.gettablelist(params).then(async res => {
        this.titleDataList = res.data.content.filter(el => {
          return el.batchId === this.activeBatchId;
        });
      });
    },
    async tableDataList(e) {
      const { data: res } = await nowpagereq.getYieldList({
        batchNo: e,
        organizationNo: sessionStorage.organizationNo,
        simpleEntity: false
      });
      if (res !== undefined) {
        return res;
      } else {
        return [];
      }
    },
    addblankmet() {
      if (this.tableData.length < 10) {
        for (let i = 0; i < 10; i++) {
          if (!this.tableData[i]?.facilityName) {
            this.tableData.push({
              facilityName: "",
              standard: {
                plantTypeName: "",
                plantVarietyName: ""
              },
              yield: "",
              productionBatchBO: {}
            });
          }
        }
        
      }
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.container {
  width: 100%;
  height: 100%;
  display: flex;
  .left {
    width: 17%;
    background-color: #00485a;
    height: 100%;
    padding-left: 12px;
    overflow: hidden;
    margin-right: 10px;
    .title {
      margin-top: 26px;
      font-size: 16px;
      font-weight: 400;
      color: #ffffff;
      text-align: center;
    }
    .left-content {
      margin: 0;
      padding: 0;
      padding-right: 12px;

      width: 100%;
      height: calc(100% - 120px);
      overflow: hidden;
      overflow-y: auto;
      li {
        border: 1px solid #2f8aa2;
        margin: 3px 10px 10px 10px;
        height: 35px;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #00485a;
        font-size: 14px;
        cursor: pointer;
      }
      .activeLi {
        background-color: #006e91;
      }
    }
  }
  .right {
    width: 83%;
    height: 100%;
    // TODO:
    // background-color: #006e91;
    .header {
      display: flex;
      height: 60px;
      width: 100%;
      li {
        position: relative;
        flex: 1;
        margin-right: 23px;
        height: 100%;
        img {
          width: 100%;
          height: 100%;
        }
        .text {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          left: 30%;
        }
      }
    }
    .right-content {
      position: relative;
      width: 100%;
      height: calc(100% - 70px);
      // overflow: hidden;
      // overflow-y: auto;
      margin-top: 10px;
      .table {
        display: block;
        width: 100%;
        // height: 400px;
      }
    }
  }
}
ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.noData {
  position: absolute;
  right: 50%;
  top: 50%;
  font-weight: 700;
  transform: translateX(-50%);
}

::v-deep {
  .el-input__inner {
    text-align: center;
  }
}
</style>
